/** 
项目管理
 **/
package com.centon.auto_code.project.controller;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.project.service.ProjectService;
import com.centon.auto_code.project.vo.ProFileVo;
import com.centon.auto_code.project.vo.ProProjectVo;
import com.centon.auto_code.project.vo.ProTableVo;
import com.centon.auto_code.project.vo.ProjectVo;
import com.centon.auto_code.project.vo.gen.SuperProProjectVo.ProjectController_addEntityInfo;
import com.centon.auto_code.project.vo.gen.SuperProProjectVo.ProjectController_deleteEntity;
import com.centon.auto_code.project.vo.gen.SuperProProjectVo.ProjectController_genProject;
import com.centon.auto_code.project.vo.gen.SuperProProjectVo.ProjectController_modifyEntityInfo;
import com.centon.auto_code.project.vo.gen.SuperProjectVo;
import com.centon.auto_code.vo.BaseRes;
import com.centon.auto_code.vo.PageData;
import javax.annotation.Resource;
import javax.annotation.Title;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("adapi/project")
public class ProjectController {


    private static Logger LOG = LoggerFactory.getLogger(ProjectController.class);

    @Resource
    private ProjectService projectService;

    /**
     * @gen_352_lwl
     * 项目详情
     * null
     * @param id
    **/
    @Title("项目详情")
    @ResponseBody
    @RequestMapping(value="entityInfo/{id}", method=RequestMethod.GET)
    public BaseRes<ProProjectVo> entityInfo(@PathVariable("id") String id) {
        return projectService.getEntityInfo(id);
    }
    /**
     * @gen_353_lwl
     * 项目删除
     * null
     * @param proProjectVo
    **/
    @Title("项目删除")
    @ResponseBody
    @RequestMapping(value="entityInfo", method=RequestMethod.DELETE)
    public BaseRes<String> deleteEntity(@RequestBody @Validated(ProjectController_deleteEntity.class) ProProjectVo proProjectVo, BindingResult bindingResult) {
        return projectService.delEntityInfo(proProjectVo.getId());
    }
    /**
     * @gen_354_lwl
     * 项目添加
     * null
     * @param proProjectVo
    **/
    @Title("项目添加")
    @ResponseBody
    @RequestMapping(value="entityInfo", method=RequestMethod.POST)
    public BaseRes<String> addEntityInfo(@RequestBody @Validated(ProjectController_addEntityInfo.class) ProProjectVo proProjectVo, BindingResult bindingResult) {
        return projectService.addEntityInfo(proProjectVo);
    }
    /**
     * @gen_355_lwl
     * 项目修改
     * null
     * @param proProjectVo
    **/
    @Title("项目修改")
    @ResponseBody
    @RequestMapping(value="entityInfo", method=RequestMethod.PUT)
    public BaseRes<String> modifyEntityInfo(@RequestBody @Validated(ProjectController_modifyEntityInfo.class) ProProjectVo proProjectVo, BindingResult bindingResult) {
        return projectService.modifyEntityInfo(proProjectVo);
    }
    /**
     * @gen_356_lwl
     * 项目生成
     * 生成项目
     * @param proProjectVo
    **/
    @Title("项目生成")
    @ResponseBody
    @RequestMapping(value="genProject", method=RequestMethod.POST)
    public BaseRes<String> genProject(@RequestBody @Validated(ProjectController_genProject.class) ProProjectVo proProjectVo, BindingResult bindingResult) {
        return projectService.genProject(proProjectVo);
    }
    /**
     * @gen_351_lwl
     * 项目列表
     * null
     * @param proProjectVo
    **/
    @Title("项目列表")
    @ResponseBody
    @RequestMapping(value="entityPage", method=RequestMethod.GET)
    public BaseRes<PageData<ProProjectVo>> entityPage(ProProjectVo proProjectVo) {
        return projectService.entityPage(proProjectVo);
    }


    /**
     * ------------------ file
     */
    /**
     * 获取项目树结构
     * @param projectId: 项目ID
     * @param packageType: Flat \ Hierachical
     * @param moduleId: 只显示对应模块
     */
    @ResponseBody
    @RequestMapping("fileTree")
    public BaseRes<String> fetchProjectFileTree(String projectId, String packageType, String moduleId) {
        LOG.info("packageType={}, moduleId={}", packageType, moduleId);
        return projectService.fetchProjectFileTree(projectId, packageType, moduleId);
    }

    @ResponseBody
    @RequestMapping(value = "fileList", method = RequestMethod.GET)
    public BaseRes<List<ProFileVo>> fetchFileByClassType(String projectId, String fileType) {
        return projectService.fetchFileByClassType(projectId, fileType);
    }

    @ResponseBody
    @RequestMapping(value = "fileInfo", method = RequestMethod.POST)
    public BaseRes<String> addFile(
            @Validated
            @RequestBody ProFileVo proFileVo,
            BindingResult bindingResult) {

        BaseRes<String> res = new BaseRes<String>();
        if( bindingResult.hasErrors() ) {
            res.setServiceCode(ServiceCode.INVALID_PARAMS);
            return res;
        }

        return projectService.addFile(proFileVo);
    }

    @ResponseBody
    @RequestMapping(value = "fileInfo", method = RequestMethod.PUT)
    public BaseRes<ProFileVo> modifyFileInfo(
            @Validated
            @RequestBody ProFileVo proFileVo,
            BindingResult bindingResult) {

        BaseRes<ProFileVo> res = new BaseRes<ProFileVo>();
        if( bindingResult.hasErrors() ) {
            res.setServiceCode(ServiceCode.INVALID_PARAMS);
            return res;
        }

        return projectService.modifyFileInfo(proFileVo);
    }

    @ResponseBody
    @RequestMapping(value = "fileInfo", method = RequestMethod.GET)
    public BaseRes<ProFileVo> fetchFileInfo(String fileId) {
        BaseRes<ProFileVo> res = projectService.fetchFileInfo(fileId);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "fileInfo", method = RequestMethod.DELETE)
    public BaseRes<String> delFileById(String fileId) {
        return projectService.delFileById(fileId);
    }

    @ResponseBody
    @RequestMapping(value = "fileListWhitField", method = RequestMethod.GET)
    public BaseRes<ProFileVo> fetchFileWithFieldByVoId(String id) {
        return projectService.fetchFileWithFieldByVoId(id);
    }


}